package com.hang.controller;

import cn.hutool.core.util.ObjectUtil;
import com.hang.common.Result;
import com.hang.entity.Department;
import com.hang.entity.User;
import com.hang.exception.MyException;
import com.hang.exception.MyExceptionEnum;
import com.hang.model.dto.request.DepartmentModelReq;
import com.hang.model.dto.request.DepartmentQueryModelReq;
import com.hang.model.dto.request.DepartmentUserDTO;
import com.hang.model.dto.request.DepartmentUserQueryDTO;
import com.hang.model.dto.response.DepartmentQueryListVO;
import com.hang.model.vo.UserInfoVO;
import com.hang.service.IDepartmentService;
import com.hang.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@Api(tags = "部门管理模块")
@RequestMapping("admin/department")
@Slf4j
public class DepartmentController {
    @Resource
    private IDepartmentService departmentService;

    @Resource
    private IUserService userService;

    @ApiOperation(value = "创建部门")
    @PostMapping("create")
    public Result<Object> createDepartment(
            @RequestBody @Valid DepartmentModelReq departmentModelReq
    ){
        try {
            departmentService.createDepartment(departmentModelReq);
            return Result.ok().message("创建成功");
        }catch (MyException me){
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "修改部门")
    @PostMapping("update")
    public Result<Object> updateDepartment(
            @RequestBody @ApiParam(value = "部门对象") @Valid DepartmentModelReq departmentModelReq
    ){
        try {
            departmentService.updateDepartment(departmentModelReq);
            return Result.ok().message("修改成功");
        }catch (MyException me){
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "删除部门")
    @DeleteMapping("delete")
    public Result<Object> deleteDepartment(
            @RequestParam("deptId") @ApiParam(value = "部门id") Long deptId
    ){
        try {
            //先寻找是否用户仍然属于该部门
            Integer count = userService.lambdaQuery().eq(User::getDeptId, deptId).count();
            if(count > 0){
                return Result.fail().message("仍有成员属于该部门,不允许删除!");
            }
            departmentService.lambdaUpdate().eq(Department::getDeptId,deptId).remove();
        }catch (Exception e){
            return Result.fail().message(e.getMessage());
        }
        return Result.ok().message("删除部门成功");
    }

    @ApiOperation(value = "按查询条件搜索部门")
    @PostMapping("queryListByCondition")
    public Result<DepartmentQueryListVO> queryListByCondition(
            @RequestBody @ApiParam(value = "部门查询条件对象",required = true) DepartmentQueryModelReq departmentQueryModelReq
    ){
        DepartmentQueryListVO departmentQueryListVO = departmentService.getDepartmentsByCondition(departmentQueryModelReq);

        return Result.ok(departmentQueryListVO);
    }

    @ApiOperation(value = "按照部门id搜索部门")
    @GetMapping("getDeptById")
    public Result<Department> getDeptById(
            @RequestParam("deptId") @ApiParam(value = "部门id") Long deptId
    ){
        Department department = departmentService.getById(deptId);
        return Result.ok(department);
    }

    @ApiOperation(value = "添加部门成员")
    @PostMapping("addUser")
    public Result<Object> addDepartmentUsers(
            @RequestBody @Valid DepartmentUserDTO departmentUserDTO
    ){
        String message = departmentService.addUser(departmentUserDTO);
        return Result.ok().message(message);
    }

    @ApiOperation(value = "移除部门成员")
    @PostMapping("removeUser")
    public Result<Object> deleteDepartmentUsers(
            @RequestBody @Valid DepartmentUserDTO departmentUserDTO
    ){
        String message = departmentService.removeUser(departmentUserDTO);
        return Result.ok().message(message);
    }

    @ApiOperation(value = "批量删除部门")
    @DeleteMapping("deleteList")
    @Transactional(rollbackFor = Exception.class)
    public Result<Object> deleteDepartmentList(@RequestParam("departmentList") List<Long> departmentList){
        if(departmentList.size()<1){
            return Result.fail().message("至少选择一个部门");
        }

        try {
            //批量搜索删除
            departmentList.forEach(this::deleteOperation);
        }catch (Exception e){
            return Result.fail().message(e.getMessage());
        }

        return Result.ok().message("删除成功");
    }

    @ApiOperation(value = "获取部门的成员列表")
    @PostMapping("userList")
    public Result<List<UserInfoVO>> getDepartmentUserInfoVOList(
            @RequestBody @Valid DepartmentUserQueryDTO departmentUserQueryDTO
    ){
        List<UserInfoVO> userInfoVOList = departmentService.getDepartmentUserByCondition(departmentUserQueryDTO);
        return Result.ok(userInfoVOList);
    }

    private void deleteOperation(Long departmentId){
        if(ObjectUtil.isEmpty(departmentId)){
            throw new MyException(MyExceptionEnum.ID_IS_NULL);
        }

        //先寻找是否用户仍然属于该部门
        Integer count = userService.lambdaQuery().eq(User::getDeptId, departmentId).count();
        if(count > 0){
            throw new MyException(MyExceptionEnum.DEPARTMENT_REMOVE_FAIL);
        }
        departmentService.lambdaUpdate().eq(Department::getDeptId,departmentId).remove();
    }
}
